//
//  SKStatusRetweetView.swift
//  WeiboProjectYL
//
//  Created by yanli on 16/7/13.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit
import SnapKit

//  转发微博视图
class SKStatusRetweetView: UIView {
    
    //  记录转发微博底部约束
    var retweetViewBottom: Constraint?
    
    //  持有的viewModel
    var statusViewModel: SKStatusViewModel? {
        
        didSet {
            //  设置转发微博内容
            contentLabel.text = statusViewModel?.retweetContent
            
            //  卸载上一次底部约束
            retweetViewBottom?.uninstall()
            
            //  判断是否有转发微博配图
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
                //  有配图
                //  设置模型
                pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                // 显示配图
                pictureView.hidden = false
            } else {
                // 没有配图(隐藏配图)
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
            
            
        }
    }
    

    // MARK: -- 懒加载控件
    private lazy var contentLabel: UILabel = {
       let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        //  多行显示
        label.numberOfLines = 0
        
        return label
    }()
    
    //  转发微博配图
    private lazy var pictureView: SKStatusPictureView = {
       
        let collectionView = SKStatusPictureView()
        
        //  设置配图的背景色跟转发微博的背景色一致
        collectionView.backgroundColor = self.backgroundColor
        
        return collectionView
    }()
    
    
    
    // 重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //  添加视图设置约束
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //  添加视图设置约束
    private func setupUI() {
        // 设置 内容label的背景图片
        contentLabel.backgroundColor = UIColor(white: 0.95, alpha: 1)
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 设置约束
        // 转发文字内容布局
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        // 转发图片视图布局
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            // 暂时写死
            //make.size.equalTo(width: 100,height: 100)
        }
        
        
        // 关键的约束
        // 转发微博视图的底部约束等于我们转发内容label的底部约束加上一个约束
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }
    
    
    
    
    
    
    
    
    
}












